package com.nbpt.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteConstraintException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.support.annotation.Nullable;

public class AccountDbHelper extends SQLiteOpenHelper {
    private String sqlSearch = "select * from tb_users";
    private static String dbName = "acccount.db";
    private String sqlCreateTBL="CREATE TABLE tb_users(\n" +
            "  id  integer  AUTO_INCREMENT PRIMARY KEY ,\n" +
            "  username VARCHAR(20) NOT NULL UNIQUE,\n" +
            "  password VARCHAR(20) NOT NULL,\n" +
            "  gender integer,\n" +
            "  phone VARCHAR(11)\n" +
            "  );";

    public AccountDbHelper(@Nullable Context context) {
        super(context, dbName, null, 1);
    }
    //查询数据库记录
    public Cursor searchAccount(){
         SQLiteDatabase sqLite = getWritableDatabase();
         Cursor cursor = sqLite.rawQuery(sqlSearch,null) ;

         return cursor;
    }//查询数据库记录
    public long insertNewAccount(String name,String password,int gender,String phone){
//        String sql = "INSERT INTO tb_users(username,password,gender,phone)VALUES('"
 //               +name+"','"+password+"',"+gender+",'"+phone+"')";
 //       try{
   //         SQLiteDatabase sqLite = getWritableDatabase();
   //         sqLite.execSQL(sql);
    //    }catch(SQLiteConstraintException e){
    //        e.printStackTrace();
     //   }
        ContentValues values = new ContentValues();
        values.put("username",name);
        values.put("password",password);
        values.put("gender",gender);
        values.put("phone",phone);
        SQLiteDatabase sqLite = getWritableDatabase();
        long ret = sqLite.insert("tb_users",null,values);
        return ret;
    }
    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
         sqLiteDatabase.execSQL(sqlCreateTBL);
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    }
}
